package edu.cicese.alz.util;

import edu.cicese.alz.TimeUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 11/06/12
 * Time: 05:42 PM
 */
public class IncenseUtil {

	public static Object loadData(String filename) {
		File file = new File(filename);
		if (file.exists()) {
			try {
				ObjectInputStream str = new ObjectInputStream(new FileInputStream(file));
				return str.readObject();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static void saveToFile(Object object, String folder, String filename) {
		try {
			File file = new File(folder);
			file.mkdirs();
			ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(new File(folder + filename)));
			stream.writeObject(object);
			stream.close();
//			System.out.println("saved to " + folder + filename);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String replaceWithNull(String value) {
		if (value.trim().equals("")) {
			return "NULL";
		}
		else {
			return value.trim();
		}
	}

	public static String getReportChartsDir(String type) {
		String outputDir = PropertyUtil.getReportChartsDir() + File.separator + type +
				File.separator + TimeUtil.getReportDate(System.currentTimeMillis()) + File.separator;
		new File(outputDir).mkdirs();

		return outputDir;
	}

	/*public static String getSystemSeparator() {
		return File.separator;
	}

	public static void checkDirs() {

	}

	public static void saveDataFile(String filename, String row) {
		try {
			PrintWriter pw = new PrintWriter(new FileWriter(filename, true));
			pw.print(row);
			pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
}
